package bainuo

import com.alibaba.fastjson.JSON

def execute() {
    def conn
    try {
        conn = groovy.sql.Sql.newInstance(mypara.hqcon(2))
        def sql ="select code ,name ,principalcode from bd_stordoc where code = '101'"
        def items = ["wareHouseNo":null,"wareHouseName":null, "storeKeeperName":null, "wareHouseType":null, "remark":null]
        def select = conn.firstRow(sql)
        if (select != null){
            items = ["wareHouseNo":select.code,"wareHouseName":select.name, "storeKeeperName":select.principalcode]
        }
        push(items)
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (conn != null) {
            conn.close()
        }
    }
}


def push(item){
    HQMHttpUtil.httpFWToMap("http://oa.bestcomm.cn:8098/wsdsjmcloud-v12/wbs/funcexe3/WMS_cangku001", item)
}
class HQMHttpUtil {
    public static HashMap<String, Object> account = new HashMap<String, Object>();

    public HQMHttps() {
    }

    public HQMHttps(HashMap<String, Object> account) {
        this.account = account;
    }
    public static Map httpFWToMap(String url, Map mapdata) {
        String json = JSON.toJSONString(mapdata);
        println "请求JSON"
        println json
        //PrintWriter out = null;
//        DataOutputStream out
        OutputStream out
        BufferedReader ins = null;
        String result = "";
        try {
            URL realUrl = new URL(url);
            // 打开和URL之间的连接
            HttpURLConnection conn = realUrl.openConnection();
            // 发送POST请求必须设置如下两行
            conn.setDoOutput(true);
            conn.setDoInput(true);
            conn.setRequestMethod("POST");
            conn.setUseCaches(false);
            // 设置通用的请求属性
            conn.setRequestProperty("Content-Type", "application/json");
            conn.setRequestProperty("connection", "Keep-Alive");

            conn.connect();

            out = conn.getOutputStream();
            if (!"".equals(json)) {
                //out.writeBytes(json);
                out.write(json.getBytes("UTF-8"));
            }
            out.flush();
            out.close();


            // 定义BufferedReader输入流来读取URL的响应
            //            ins = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
            if (conn.getResponseCode() == 200) {
                ins = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
            } else {
                ins = new BufferedReader(new InputStreamReader(conn.getErrorStream(), "UTF-8"));
            }

            String line;
            while ((line = ins.readLine()) != null) {
                result += line;
            }
            println("调用WMS返回报文="+result)
            return JSON.parseObject(result, Map.class);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("[POST请求]向地址：" + url + " 发送数据：" + json + " 发生错误!" + result + "JSON结束");
        } finally {// 使用finally块来关闭输出流、输入流
            try {
                if (out != null) {
                    out.close();
                }
                if (ins != null) {
                    ins.close();
                }
            } catch (IOException ex) {
                System.out.println("关闭流异常");
            }
        }
        return null;
    }
}